package com.wyh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyh.common.response.PageInfo;
import com.wyh.entity.AdminUserLevel;
import com.wyh.mapper.AdminUserLevelMapper;
import com.wyh.request.AdminUserLevelRequest;
import com.wyh.service.IAdminUserLevelService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户管理业务层处理
 *
 * @author WangYuHang
 * @date 2024-03-03
 */
@Slf4j
@Service
@AllArgsConstructor
public class AdminUserLevelServiceImpl implements IAdminUserLevelService {

    private final AdminUserLevelMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AdminUserLevel add(AdminUserLevelRequest request) {
        AdminUserLevel entity = AdminUserLevel.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Integer id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Integer> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(AdminUserLevelRequest request) {
        AdminUserLevel entity = AdminUserLevel.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0 ;
    }

    @Override
    public AdminUserLevel getById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<AdminUserLevel> pageList(AdminUserLevelRequest request) {
        Page<AdminUserLevel> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<AdminUserLevel> query = this.buildWrapper(request);
        IPage<AdminUserLevel> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<AdminUserLevel> list(AdminUserLevelRequest request) {
        LambdaQueryWrapper<AdminUserLevel> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<AdminUserLevel> buildWrapper(AdminUserLevelRequest param) {
        LambdaQueryWrapper<AdminUserLevel> query = new LambdaQueryWrapper<>();
         if (StringUtils.isNotBlank(param.getLevelName())){
               query.like(AdminUserLevel::getLevelName ,param.getLevelName());
        }
        if (param.getLevel() != null){
               query.eq(AdminUserLevel::getLevel ,param.getLevel());
        }
        if (param.getType() != null){
               query.eq(AdminUserLevel::getType ,param.getType());
        }
        if (param.getMinExp() != null){
               query.eq(AdminUserLevel::getMinExp ,param.getMinExp());
        }
        if (param.getMaxExp() != null){
               query.eq(AdminUserLevel::getMaxExp ,param.getMaxExp());
        }
        if (param.getCreateUserId() != null){
               query.eq(AdminUserLevel::getCreateUserId ,param.getCreateUserId());
        }
        if (param.getUpdateUserId() != null){
               query.eq(AdminUserLevel::getUpdateUserId ,param.getUpdateUserId());
        }
        return query;
    }

}
